<?php

namespace common\services\picture;

use common\error\ErrorCode;
use common\services\BaseService;
use common\repositories\picture\PictureRepository;

class PictureService extends BaseService {
    public $_pictureRepository;
    public function __construct(PictureRepository $pictureRepository, $config = []) {
        $this->_pictureRepository = $pictureRepository;
        parent::__construct($config);
    }

    /**
     * 保存图片信息
     * @param $data array 图片信息
     * @return bool
     */
    public function save($data)
    {
        $saveResult =  $this->_pictureRepository->save($data);
        if ($saveResult === false){
            self::setAndReturn(ErrorCode::ERR_PICTURE_SAVE_FALL,"保存或新增失败");
        }
        return $saveResult;
    }

    /**
     * 根据id取图片信息
     * @param $id int id
     * @return array|null|\yii\db\ActiveRecord
     */
    public function getPicInfoById($id){
        $picInfo =  $this->_pictureRepository->getPicInfoById($id);
        if (empty($picInfo)){
            self::setAndReturn(ErrorCode::ERR_PICTURE_INVALID_FALL,"无此id图片");
        }
        return $picInfo;
    }

    /**
     * 根据id删除图片
     * @param $id
     * @return false|int
     */
    public function delete($id)
    {
        $deleteResult = $this->_pictureRepository->deleteById($id);
        if ($deleteResult === false){
            self::setAndReturn(ErrorCode::ERR_PICTURE_INVALID_FALL, "删除图片失败");
        }
        return $deleteResult;
    }

    /**
     * 更新启用状态
     * @param $id int id
     * @param $status int is_able
     * @return bool
     */
    public function updateAble($id, $status)
    {
        $updateResult = $this->_pictureRepository->updateAble($id, $status);
        if ($updateResult === false){
            self::setAndReturn(ErrorCode::ERR_PICTURE_SAVE_FALL, "修改启用状态失败");
        }
        return $updateResult;
    }

    /**
     * 后台图片列表
     * @param $keyword  string 关键字
     * @param $page int 页码
     * @param $perPage int 条数
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getPicList($keyword, $page, $perPage)
    {
        return $this->_pictureRepository->getPicList($keyword, $page, $perPage);
    }
}